package com.cgsrp.cgsrp_baby.service.impl;

import com.cgsrp.cgsrp_baby.dao.ChildParentRelationMapper;
import com.cgsrp.cgsrp_baby.domain.ChildParentRelation;
import com.cgsrp.utils.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_baby.domain.Message;
import com.cgsrp.cgsrp_baby.dao.MessageMapper;
import com.cgsrp.cgsrp_baby.service.MessageService;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService{

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private ChildParentRelationMapper childParentRelationMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return messageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Message record) {
        return messageMapper.insert(record);
    }

    @Override
    public int insertSelective(Message record) {
        return messageMapper.insertSelective(record);
    }

    @Override
    public Message selectByPrimaryKey(Integer id) {
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Message record) {
        return messageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Message record) {
        return messageMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Message> list(int userId) {
        List<Message> list =  messageMapper.selectByUserId(userId);
        List<Message> systemMessage = messageMapper.selectByType(2);
        for (Message message : list) {
            if (message.getType()==0){
                ChildParentRelation childParentRelation = childParentRelationMapper.selectByPrimaryKey(message.getRelationId());
                message.setChildParentRelation(childParentRelation);
            }
        }
        list.addAll(systemMessage);
        return list;
    }

    @Override
    public int updateType(Integer messageId, Integer type) {
        return messageMapper.updateType(messageId,type);
    }

    @Override
    public int updateState(Integer messageId, Integer state) {
        return messageMapper.updateState(messageId,state);
    }

    @Override
    public Result del(Integer id) {
        return messageMapper.deleteByPrimaryKey(id)>0?Result.success("删除成功"):Result.failure("删除失败");
    }

}
